package test.api;

import java.util.ArrayList;
import java.util.List;

import keter.mongo.model.Book;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;

public class PlanDao {
	
    String collection = "mongoapi";

    public ObjectId add(Plan plan) {
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(collection);
        DBObject object = DBObjectUtil.getDBObjectByPlan(plan);
        coll.insert(object, WriteConcern.SAFE);
        return (ObjectId) object.get("_id");
    }

    /**
     * 通过ID获得一个plan对象
     * 
     * @param id
     */
    public Plan getById(String id) {
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(collection);
        DBObject object = coll.findOne(new BasicDBObject("_id", new ObjectId(id)));
        return getPlanByDBObject(object);
    }
    
    public List<Plan> findByName(String name) {
        DBObject dbo = new BasicDBObject();
        dbo.put("name", name);
        List<Plan> planList = new ArrayList<Plan>();
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(collection);
        DBCursor cursor = coll.find(dbo);
        try {
            while (cursor.hasNext()) {
                DBObject object = cursor.next();
                planList.add(getPlanByDBObject(object));
            }
        } finally {
            cursor.close();
        }
        return planList;
    }
    
    /**
     * 通过BasicDBObject获得Plan对象
     * 
     * @param object
     */
    private static Plan getPlanByDBObject(DBObject object) {
        Plan plan = new Plan();
        plan.setId(((ObjectId) object.get("_id")).toString());
        plan.setName((String) object.get("name"));
//        plan.setUsers( object.get("users"));
        return plan;
    }
    
    private static List<Book> getUsersFromJson(String jsonStr){
//        JSON.parseArray(jsonStr)
        return null;
    }

    /**
     * 删除一个对象
     * 
     * @param id
     */
    public void delete(String id) {
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(collection);
        coll.remove(new BasicDBObject("_id", new ObjectId(id)));
    }

    /**
     * 获得所有的plan对象
     * 
     * @return
     */
    public List<Plan> getAll() {
        List<Plan> planList = new ArrayList<Plan>();
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(collection);
        DBCursor cursor = coll.find();
        try {
            while (cursor.hasNext()) {
                DBObject object = cursor.next();
                planList.add(getPlanByDBObject(object));
            }
        } finally {
            cursor.close();
        }
        return planList;
    }

    /**
     * 修改一个方案
     * 
     * @param plan
     */
    public void modify(Plan plan) {
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(collection);
        coll.update(new BasicDBObject("_id", new ObjectId(plan.getId())),
                DBObjectUtil.getDBObjectByPlan(plan));
    }

}
